package com.example.myapplication.CustomView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.style.BackgroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

import com.example.myapplication.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MyView extends LinearLayout implements View.OnClickListener{
    private LinearLayout.LayoutParams params;

    private List<View> list;
    private ImageView imageView;
    private TextView textView;
    private ClickBarListener barListener ;
    private View beforeview;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void init() {
        list = new ArrayList<>();
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);

        params = new LayoutParams(0, 110);
        params.weight = 2;
    }

    public MyView addTextandColor(String text){
        addText(text,0xFF999999,0xFF333333);
        return  this;
    }

    public MyView addImage(int imgid) {
        imageView = new ImageView(getContext());
        imageView.setLayoutParams(params);
        imageView.setImageResource(imgid);
        imageView.setPadding(5,5,5,5);

        imageView.setTag(R.id.bottom_bar_type_key,1);
        list.add(imageView);
        return this;
    }
    public MyView addText(String text,int select,int unselect) {
        textView = new TextView(getContext());
        textView.setLayoutParams(params);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(20);

        textView.setTextColor(unselect);
        textView.setTag(R.id.bottom_bar_normal_color_key,unselect);
        textView.setTag(R.id.bottom_bar_select_color_key,select);
        textView.setTag(R.id.bottom_bar_type_key,2);


        list.add(textView);
        return this;
    }

    //监听点击事件
    public MyView listener(ClickBarListener clickBarListener) {
        this.barListener  = clickBarListener;
        return this;
    }
    //接口返回点击的下标
    public interface ClickBarListener {
        void onChickItem(int position);
    }
    //展示  文字和  图片
    public void show() {
        for (int i = 0; i <list.size() ; i++) {
            addView(list.get(i));
            list.get(i).setTag(R.id.bottom_bar_position_color_key,i);
            list.get(i).setOnClickListener(this);
    }
        //第一个直接选中
        list.get(0).setTag(R.id.bottom_bar_type_key,2);
        style(list.get(0));
        beforeview = list.get(0);

    }


    //点击方法
    @Override
    public void onClick(View v) {
        if (beforeview != null && (int) v.getTag(R.id.bottom_bar_position_color_key) == (int) beforeview.getTag(R.id.bottom_bar_position_color_key)) {
            return;
        }
        if (barListener  !=null){
            barListener .onChickItem((int) v.getTag(R.id.bottom_bar_position_color_key));
        }
        style(v);
       beforeview = v;
    }
    private void style(View selectView) {
        switch ((int) selectView.getTag(R.id.bottom_bar_type_key)) {
            case 1:
                //Image
                styleImage(selectView,true);
                break;
            case 2:
                //Text
                styleText(selectView, true);
                break;
        }
        if (beforeview == null) {
            return;
        }
        switch ((int) beforeview.getTag(R.id.bottom_bar_type_key)) {
            case 1:
                //Image
                styleImage(beforeview,false);
                break;
            case 2:
                //Text
                styleText(beforeview, false);
                break;
        }
    }

    //切换图片样式
    private void styleImage(View selectView, boolean b) {
        if (b){
            ((ImageView)selectView).setImageResource(R.mipmap.xiangji_hui);

        }else{
            ((ImageView)selectView).setImageResource(R.mipmap.xiangji_hei);
        }
    }
    //切换文字样式
    private void styleText(View view, boolean isSelect) {
        if (isSelect) {

            ((TextView) view).setTextColor((int) view.getTag(R.id.bottom_bar_select_color_key));
            ((TextView) view).setTextSize(30);
            ((TextView) view).setTypeface(Typeface.DEFAULT_BOLD);
        } else {

            ((TextView) view).setTextColor((int) view.getTag(R.id.bottom_bar_normal_color_key));
            ((TextView) view).setTextSize(20);
            ((TextView) view).setTypeface(Typeface.DEFAULT);
        }
    }


}
